//
//  WCChooseItem.m
//  CeshiCoupons
//
//  Created by wsj2015 on 15/12/8.
//  Copyright © 2015年 wsj2015. All rights reserved.
//

#import "WCChooseItem.h"

@implementation WCChooseItem
{
    NSInteger _selectIndex;
 
}
+ (instancetype)viewFromNib
{
    return [[[NSBundle mainBundle] loadNibNamed:@"WCChooseItem" owner:nil options:0]lastObject];
 
}

- (void)awakeFromNib
{
    [self.unsedBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self.unsedBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.unusedBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self.unusedBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.unusedBtn.selected = YES;
    _selectIndex = 1;
    [self.unusedBtn addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.unsedBtn addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)chooseAction:(UIButton *)btn
{
    UIButton *preBtn =(UIButton *)[self viewWithTag:_selectIndex];
    if (preBtn == btn) {
        return;
    }
    self.unsedBtn.enabled = NO;
    self.unusedBtn.enabled = NO;
    preBtn.selected = NO;
    btn.selected = !btn.selected;
    _selectIndex = btn.tag;
    __block CGRect frame = self.lineView.frame;
    
    if (_selectIndex ==2) {
        frame.origin.x += frame.size.width;
       
        frame.size.width -= 150;;
        
        [UIView animateWithDuration:0.35f animations:^{
             self.lineView.frame = frame;
            
        } completion:^(BOOL finished) {
            frame.size.width +=150;
            [UIView animateWithDuration:0.35f animations:^{
                self.lineView.frame = frame;
                
            } completion:^(BOOL finished) {
                self.unsedBtn.enabled = YES;
                self.unusedBtn.enabled = YES;
                
            }];
            
        }];
      
    }else{
        
         frame.size.width -= 150;;
        frame.origin.x -= 150;
        
        [UIView animateWithDuration:0.35f animations:^{
            self.lineView.frame = frame;
            
        } completion:^(BOOL finished) {
            frame.size.width +=150;
            [UIView animateWithDuration:0.35f animations:^{
                self.lineView.frame = frame;
                
            } completion:^(BOOL finished) {
                
                self.unsedBtn.enabled = YES;
                self.unusedBtn.enabled = YES;
            }];
            
        }];
    
    }
    if (self.itemCallBack) {
        self.itemCallBack(_selectIndex);
    }
    

}
@end
